using System;
using System.IO;
using System.Xml;

namespace HabilisDesign.Iota
{
    public class User
    {
        #region Fields

        private string m_Id;
        private string m_Name;
        private string m_ScreenName;
        private string m_Location;
        private string m_Description;
        private Uri m_ProfileImage;
        private Uri m_Url;
        private bool m_Protected;
        private Status m_Status;

        #endregion

        #region Properties

        public string Id
        {
            get
            {
                return m_Id;
            }
        }

        public string Name
        {
            get
            {
                return m_Name;
            }
        }

        public string ScreenName
        {
            get
            {
                return m_ScreenName;
            }
        }

        public string Location
        {
            get
            {
                return m_Location;
            }
        }

        public string Description
        {
            get
            {
                return m_Description;
            }
        }

        public Uri ProfileImage
        {
            get
            {
                return m_ProfileImage;
            }
        }

        public Uri Url
        {
            get
            {
                return m_Url;
            }
        }

        public bool Protected
        {
            get
            {
                return m_Protected;
            }
        }

        public string StatusText
        {
            get
            {
                return m_Status.Text;
            }
        }

        public Status CurrentStatus
        {
            get
            {
                return m_Status;
            }
        }
        
        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the User class.
        /// </summary>
        private User()
        {
        }
        
        #endregion

		#region Methods

		/// <summary>
		/// Gets the URL.
		/// </summary>
		/// <param name="url">The URL.</param>
		/// <returns></returns>
		private static Uri GetUrl( string url )
		{
			if ( !string.IsNullOrEmpty( url ) )
			{
				return new Uri( url );
			}
			return null;
		}

		/// <summary>
		/// Parses the specified XML fragment.
		/// </summary>
		/// <param name="xmlFragment">The XML fragment.</param>
		/// <returns></returns>
		public static User Parse( string xmlFragment )
		{
			User user = new User();
			XmlReaderSettings settings = new XmlReaderSettings();
			settings.IgnoreProcessingInstructions = true;
			settings.IgnoreWhitespace = true;
			settings.IgnoreComments = true;
			settings.ConformanceLevel = ConformanceLevel.Fragment;
			settings.CloseInput = true;
			using ( XmlReader reader = XmlReader.Create( new StringReader( xmlFragment ), settings ) )
			{
				while ( reader.Read() )
				{
					if ( reader.IsStartElement( "id" ) )
					{
						reader.Read();
						user.m_Id = reader.Value;
					}
					if ( reader.IsStartElement( "name" ) )
					{
						reader.Read();
						user.m_Name = reader.Value;
					}
					if ( reader.IsStartElement( "screen_name" ) )
					{
						reader.Read();
						user.m_ScreenName = reader.Value;
					}
					if ( reader.IsStartElement( "location" ) )
					{
						reader.Read();
						user.m_Location = reader.Value;
					}
					if ( reader.IsStartElement( "description" ) )
					{
						reader.Read();
						user.m_Description = reader.Value;
					}
					if ( reader.IsStartElement( "profile_image_url" ) )
					{
						reader.Read();
						user.m_ProfileImage = GetUrl( reader.Value );
					}
					if ( reader.IsStartElement( "url" ) )
					{
						reader.Read();
						user.m_Url = GetUrl( reader.Value );
					}
					if ( reader.IsStartElement( "protected" ) )
					{
						reader.Read();
						user.m_Protected = Convert.ToBoolean( reader.Value );
					}
					if ( reader.IsStartElement( "status" ) )
					{
						user.m_Status = Status.Parse( reader.ReadInnerXml() );
					}
				}
				reader.Close();
			}
			return user;
		}

		#endregion

    } // end class

} // end namespace
